package com.flow.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.flow.dao.IUserRoleDao;
import com.flow.model.RoleTab;
import com.flow.model.UserBasic;
import com.flow.model.UserRole;
import com.flow.service.IUserRoleService;
import com.flow.tools.StringTools;

/**
 * 
 * @author Wang
 * @param <T>
 */
public class UserRoleService<T> implements IUserRoleService<T> {

	/**
	 * Dao层引用
	 */
	private IUserRoleDao<T> urDao;

	/**
	 *  
	 */
	public boolean updateUserRole(String rtId, List<Integer> checkedStatus,
			String ubId) {

		try {
			
			List<Integer> removeList = new ArrayList<Integer>();

			if ((checkedStatus == null) || (!StringTools.nullOrEmpty(ubId)))
				return false;

			List<Integer> idList;
			if (StringTools.nullOrEmpty(rtId)) {
				String[] arr = rtId.split(",");
				idList = new ArrayList<Integer>();
				for (int i = 0; i < arr.length; i++) {
					if (StringTools.nullOrEmpty(arr[i]))
						idList.add(Integer.valueOf(arr[i]));
				}

				for (int i = 0; i < idList.size(); i++) {
					if (checkedStatus.indexOf(idList.get(i)) == -1)
						removeList.add((Integer) idList.get(i));
				}

				for (int i = 0; i < checkedStatus.size();) {
					if (idList.indexOf(checkedStatus.get(i)) != -1) {
						checkedStatus.remove(i);
						i = 0;
					} else {
						i++;
					}

				}

				if (removeList != null) {
					for (Integer integer : removeList) {
						String hql = "delete UserRole where userBasic.ubId= ? and roleTab.rtId = ?";
						List<Object> params = new ArrayList<Object>();
						params.add(ubId);
						params.add(integer);
						if (urDao.universalMethodUserRole(hql, params) == 0)
							return false;
					}
				}

			}

			if (checkedStatus != null) {
				for (Integer integer : checkedStatus) {
					RoleTab rt = new RoleTab();
					rt.setRtId(integer);
					UserBasic ub = new UserBasic();
					ub.setUbId(ubId);
					UserRole userRole = new UserRole(rt, ub);
					this.urDao.insertUserRole(userRole);
				}
			}
			return true;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return false;
	}

	/**
	 * 根据ID查询用户角色信息
	 */
	public List<T> findUserRoleById(String ubId) {

		List<T> list = null;

		try {

			if (!StringTools.nullOrEmpty(ubId)) {
				return null;
			}

			if (ubId == null) {
				String hql = "from UserRole";
				list = urDao.findUserRoleAllByHql(hql);
			} else {
				String hql = "from UserRole where userBasic.ubId = ?";
				list = urDao.findUserRoleAllByHql(hql, ubId);
			}

		} catch (Exception e) {

			e.printStackTrace();

		}

		return list;
	}
	
	/**
	 * 根据用户ID查询用户角色信息
	 */
	@Override
	public List<T> findUserRoleByUbId(String ubId) {

		if(!StringTools.nullOrEmpty(ubId))
			return null;
		
		try {
			
			String hql = " from UserRole where userBasic.ubId = ?";
			return urDao.findUserRoleAllByHql(hql, ubId);
			
		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return null;
	}

	
	
	public void setUrDao(IUserRoleDao<T> urDao) {
		this.urDao = urDao;
	}

	

}
